@model VotingSystem.Models.Survey
@using VotingSystem.ViewModels
@using VotingSystem.Models

@{
    ViewData["Title"] = "问卷详情";
    var source = ViewData["Source"] as string;
    var showResponses = ViewData["ShowResponses"] as bool? ?? false;
    var statistics = ViewData["Statistics"] as SurveyStatisticsViewModel;
}

<div class="container py-4">
    <div class="row">
        <div class="col-md-8">
            <div class="card mb-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h2 class="mb-0">@Model.Title</h2>
                    @if (User.Identity?.Name == Model.Creator?.UserName)
                    {
                        <div class="dropdown">
                            <button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
                                操作
                            </button>
                            <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                                <li><a class="dropdown-item" href="#" onclick="copyShareLink(); return false;">复制分享链接</a></li>
                                <li><a class="dropdown-item" asp-action="Edit" asp-route-id="@Model.Id">编辑问卷</a></li>
                                <li><a class="dropdown-item" asp-action="Delete" asp-route-id="@Model.Id">删除问卷</a></li>
                            </ul>
                        </div>
                    }
                </div>
                <div class="card-body">
                    <p class="card-text text-muted">@Model.Description</p>
                    <div class="d-flex gap-3 text-muted small">
                        <span><i class="fas fa-calendar me-1"></i>创建时间：@Model.CreatedAt.ToString("yyyy-MM-dd HH:mm")</span>
                        <span><i class="fas fa-users me-1"></i>回答数：@Model.Responses.Count</span>
                        <span><i class="fas fa-eye me-1"></i>@(Model.IsPublic ? "公开" : "仅自己可见")</span>
                    </div>
                </div>
            </div>

            @if (showResponses)
            {
                <div class="card mb-4">
                    <div class="card-body">
                        <h3 class="card-title">填写结果</h3>
                        @foreach (var response in Model.Responses.OrderByDescending(r => r.SubmitTime))
                        {
                            <div class="card mb-3">
                                <div class="card-header">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <a href="@Url.Action("ViewResponse", "Surveys", new { responseId = response.Id })">@(Model.IsAnonymous ? "匿名用户" : response.Respondent?.UserName ?? "未知用户")</a>
                                        <span>提交时间：@response.SubmitTime.ToString("yyyy-MM-dd HH:mm:ss")</span>
                                    </div>
                                </div>
                                <div class="card-body">
                                    @foreach (var questionResponse in response.QuestionResponses.OrderBy(qr => qr.Question?.Order))
                                    {
                                        <div class="card mb-3">
                                            <div class="card-header">
                                                <h5>@(questionResponse.Question?.QuestionText ?? "未知问题")</h5>
                                            </div>
                                            <div class="card-body">
                                                @if (questionResponse.Question?.Type == QuestionType.Text)
                                                {
                                                    <p>作答：@questionResponse.TextResponse</p>
                                                }
                                                else if (questionResponse.Question?.Type == QuestionType.Rating)
                                                {
                                                    <p>评分：@questionResponse.RatingResponse</p>
                                                }
                                                else
                                                {
                                                    <p>原题目选项：</p>
                                                    <ul class="list-group">
                                                        @foreach (var option in questionResponse.Question.Options)
                                                        {
                                                            <li class="list-group-item">@option.OptionText</li>
                                                        }
                                                    </ul>
                                                    <p>你的选择：</p>
                                                    <ul class="list-group">
                                                        @foreach (var selectedOption in questionResponse.SelectedOptions)
                                                        {
                                                            <li class="list-group-item">@selectedOption.Option?.OptionText</li>
                                                        }
                                                    </ul>
                                                }
                                            </div>
                                        </div>
                                    }
                                </div>
                            </div>
                        }
                    </div>
                </div>
            }
            else
            {
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">问题列表</h5>
                    </div>
                    <div class="card-body">
                        @foreach (var question in Model.Questions.OrderBy(q => q.Order))
                        {
                            <div class="mb-4">
                                <h6 class="mb-2">@question.QuestionText</h6>
                                <p class="text-muted">问题类型：@question.Type</p>
                                @if (question.Type == QuestionType.SingleChoice || question.Type == QuestionType.MultipleChoice)
                                {
                                    <div class="list-group">
                                        @foreach (var option in question.Options.OrderBy(o => o.Order))
                                        {
                                            <div class="list-group-item">
                                                <span>@option.OptionText</span>
                                            </div>
                                        }
                                    </div>
                                }
                            </div>
                        }
                    </div>
                </div>
            }
        </div>

        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">填写用户列表</h5>
                </div>
                <div class="card-body">
                    @if (Model.Responses.Any())
                    {
                        <div class="list-group">
                            @foreach (var response in Model.Responses.OrderByDescending(r => r.SubmitTime))
                            {
                                <a href="@Url.Action("ViewResponse", "Surveys", new { responseId = response.Id })" 
                                   class="list-group-item list-group-item-action">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div>
                                            <h6 class="mb-1">@(Model.IsAnonymous ? "匿名用户" : response.Respondent?.UserName ?? "未知用户")</h6>
                                            <small class="text-muted">@response.SubmitTime.ToString("yyyy-MM-dd HH:mm")</small>
                                        </div>
                                        <i class="fas fa-chevron-right text-muted"></i>
                                    </div>
                                </a>
                            }
                        </div>
                    }
                    else
                    {
                        <p class="text-muted text-center my-3">暂无填写用户</p>
                    }
                </div>
            </div>
        </div>
    </div>

    <div class="card-footer bg-transparent">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <a asp-action="Edit" asp-route-id="@Model.Id" class="btn btn-primary">
                    <i class="fas fa-edit me-1"></i>编辑
                </a>
                <a asp-action="Index" class="btn btn-secondary">
                    <i class="fas fa-arrow-left me-1"></i>返回列表
                </a>
            </div>
            <div>
                <div class="btn-group">
                    <button type="button" class="btn btn-outline-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fas fa-download me-1"></i>导出
                    </button>
                    <ul class="dropdown-menu">
                        <li><a class="dropdown-item" asp-action="Export" asp-route-id="@Model.Id" asp-route-format="pdf">
                            <i class="fas fa-file-pdf me-1"></i>导出为PDF
                        </a></li>
                        <li><a class="dropdown-item" asp-action="Export" asp-route-id="@Model.Id" asp-route-format="excel">
                            <i class="fas fa-file-excel me-1"></i>导出为Excel
                        </a></li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 分享模态框 -->
<div class="modal fade" id="shareModal" tabindex="-1" aria-labelledby="shareModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="shareModalLabel">分享问卷</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="mb-3">
                    <label for="shareUrl" class="form-label">分享链接</label>
                    <div class="input-group">
                        <input type="text" class="form-control" id="shareUrl" readonly>
                        <button class="btn btn-outline-secondary" type="button" id="copyBtn">
                            <i class="fas fa-copy"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@section Styles {
    <style>
        .list-group-item-action:hover {
            background-color: #f8f9fa;
        }
        .list-group-item-action .fas {
            transition: transform 0.2s;
        }
        .list-group-item-action:hover .fas {
            transform: translateX(3px);
        }
    </style>
}

@section Scripts {
    <script>
        $(document).ready(function() {
            // 分享按钮点击事件
            $('.share-btn').click(function() {
                var id = $(this).data('id');
                var title = $(this).data('title');
                var shareUrl = window.location.origin + '/Surveys/Fill/' + id;
                
                $('#shareUrl').val(shareUrl);
                $('#shareModal').modal('show');
            });

            // 复制链接按钮点击事件
            $('#copyBtn').click(function() {
                var shareUrl = $('#shareUrl');
                shareUrl.select();
                document.execCommand('copy');
                
                // 显示复制成功提示
                var btn = $(this);
                var originalText = btn.html();
                btn.html('<i class="fas fa-check"></i>');
                setTimeout(function() {
                    btn.html(originalText);
                }, 2000);
            });
        });

        function copyShareLink() {
            var link = window.location.origin + '/Surveys/Details/@Model.Id?token=@Model.AccessToken';
            navigator.clipboard.writeText(link).then(function() {
                alert('分享链接已复制到剪贴板！');
            }).catch(function(err) {
                console.error('复制失败：', err);
                alert('复制失败，请手动复制链接：' + link);
            });
        }
    </script>
} 